#include "block.h"

Block::Block(BlockType blockType,Variant subType, QObject *parent) : QObject(parent)
{
    setType(blockType, subType);
}

Block::~Block(){

}
/**
 * 设置(根据不同的类型改变数组中的值创建不同的形状)
 * @brief setType I, J, L, O, S, T, Z
 * @param blockType
 * @param subType
 */
void Block::setType(BlockType blockType, Variant subType){
    this->type = blockType;
    this->subType = subType;
    createType();

}

/**
 * 设置为0
 * @brief Block::reSet
 */
void Block::reSet(){
    for(int i = 0; i < 4;i++){
        for(int j = 0; j < 4; j++){
            block[i][j] = 0;
        }
    }
}

/**
 * 创建设置的形状类型
 * @brief Block::createType
 */
void Block::createType(){
    reSet();
    switch (type) {
        case BlockType::I:
            createTypeI();
         break;
        case BlockType::J:
            createTypeJ();
         break;
        case BlockType::L:
             createTypeL();
         break;
        case BlockType::O:
             createTypeO();
         break;
        case BlockType::S:
             createTypeS();
         break;
        case BlockType::T:
             createTypeT();
         break;
        case BlockType::Z:
             createTypeZ();
         break;
    }
}

/**
 * 变形
 * @brief Block::change
 */
void Block::change(){
    switch (subType) {
        case Variant::LEFT:
            subType = Variant::UP;
            break;
        case Variant::UP:
            subType = Variant::RIGHT;
            break;
        case Variant::RIGHT:
            subType = Variant::DOWN;
            break;
        case Variant::DOWN:
            subType = Variant::LEFT;
            break;
    }
    createType();

}

/**
 * 复制形状
 * @brief Block::copy
 * @param block
 */
void Block::copy(Block &block){
    setType(block.type, block.subType);
}

void Block::createTypeI(){
    if(subType == Variant::LEFT || subType == Variant::RIGHT){ //都是纵向
        block[0][1]=1;
        block[1][1]=1;
        block[2][1]=1;
        block[3][1]=1;
    }else{ //横向
        block[1][0]=1;
        block[1][1]=1;
        block[1][2]=1;
        block[1][3]=1;
    }
}
void Block::createTypeJ(){
    switch (subType) {
        case Variant::LEFT:
            block[0][2]=1;
            block[1][2]=1;
            block[2][2]=1;
            block[2][1]=1;
            return;
        case Variant::UP:
            block[1][1]=1;
            block[2][1]=1;
            block[2][2]=1;
            block[2][3]=1;
            return;
        case Variant::RIGHT:
            block[0][1]=1;
            block[0][2]=1;
            block[1][1]=1;
            block[2][1]=1;
            return;
        case Variant::DOWN:
            block[1][0]=1;
            block[1][1]=1;
            block[1][2]=1;
            block[2][2]=1;
            return;
    }


}
void Block::createTypeL(){
    switch (subType) {
        case Variant::LEFT:
            block[0][1]=1;
            block[1][1]=1;
            block[2][1]=1;
            block[2][2]=1;
            return;
        case Variant::UP:
            block[1][1]=1;
            block[1][2]=1;
            block[1][3]=1;
            block[2][1]=1;
            return;
        case Variant::RIGHT:
            block[0][1]=1;
            block[0][2]=1;
            block[1][2]=1;
            block[2][2]=1;
            return;
        case Variant::DOWN:
            block[1][2]=1;
            block[2][2]=1;
            block[2][1]=1;
            block[2][0]=1;
            return;
    }
}
void Block::createTypeO(){ 
            block[1][1]=1;
            block[1][2]=1;
            block[2][1]=1;
            block[2][2]=1;
}
void Block::createTypeS(){
    if(subType == Variant::LEFT || subType == Variant::RIGHT){
        block[0][1]=1;
        block[1][1]=1;
        block[1][2]=1;
        block[2][2]=1;
    }else{
        block[1][1]=1;
        block[1][2]=1;
        block[2][0]=1;
        block[2][1]=1;
    }
}
void Block::createTypeT(){
    switch (subType) {
        case Variant::LEFT:
            block[1][0]=1;
            block[1][1]=1;
            block[1][2]=1;
            block[2][1]=1;
            return;
        case Variant::UP:
            block[0][2]=1;
            block[1][2]=1;
            block[2][2]=1;
            block[1][1]=1;
            return;
        case Variant::RIGHT:
            block[1][1]=1;
            block[2][0]=1;
            block[2][1]=1;
            block[2][2]=1;
            return;
        case Variant::DOWN:
            block[0][1]=1;
            block[1][1]=1;
            block[2][1]=1;
            block[1][2]=1;
            return;
    }
}
void Block::createTypeZ(){
    if(subType == Variant::LEFT || subType == Variant::RIGHT){
        block[1][0]=1;
        block[1][1]=1;
        block[2][1]=1;
        block[2][2]=1;
    }else{
        block[0][2]=1;
        block[1][2]=1;
        block[1][1]=1;
        block[2][1]=1;
    }
}
